
/*
 * jQuery Web Page Function @requires jQuery v1.4+
 * Dual licensed under the MIT and GPL licenses.
 *
 * Autor: Leo
 * Version: 3.0
 * For Common Web Page .
 */
;(function(){
    $.fn.rating = function (options) {
        // Method calling logic
        if ($.rating[options]) {
            return $.rating[options].apply(this,Array.prototype.slice.call(arguments,1));
        } else if (typeof options === 'object' || !options) {
            return $.rating.init.apply(this,arguments);
        } else {
            $.error('Method ' +  options + ' does not exist on jQuery.rating');
        }
    };
	$.rating = {
		init:function(options){
			var opts = $.extend({},$.rating.options,options);
			return this.each(function(n) {
				width = (opts.value/opts.maxValue)*$(this).width();
				
				$(this).data('$input',$('<input name="'+((opts.id)?opts.id:this.id)+'" type="hidden"/>').appendTo(this).val(opts.value));
				$(this).data('$slider',$('<span></span>').appendTo(this)
					.css({
						width:width,
						display: 'block',
						cursor:'pointer',
						height:$(this).height()
						}));
				$(this).data('options',opts)
				.mousemove($.rating._mousemove)
				.mouseout($.rating._mouseout)
				.click($.rating._click)
				$.rating.value.apply(this,[opts.value]);
			})
		},
		_slidermove:function(width){
			opts = $(this).data('options');
			$slider = $(this).data('$slider');
			step = (opts.step/opts.maxValue)*$(this).width()
			width = Math.ceil(width/step)*step;
			$slider.width(width);			
		},
		_mousemove : function(e){
			opts = $(this).data('options');
			if(!opts.readonly)$.rating._slidermove.apply(this,[e.pageX-$(this).offset().left]);
		},
		_mouseout : function(e){
			val = $(this).data('$input').val();
			opts = $(this).data('options');
			width = (val/opts.maxValue)*$(this).width();
			$.rating._slidermove.apply(this,[width]);
		},
		_change : function(width,callback){
			$.rating._slidermove.apply(this,[width]);
			if($.isFunction(opts.onchange))opts.onchange($(this).data('$input').val());
		},
		_click : function(e){
			opts = $(this).data('options')
			if(!opts.readonly){
				var width = e.pageX-$(this).offset().left,
					val = Math.ceil(width/$(this).width()*opts.maxValue/opts.step)*opts.step;
				$(this).data('$input').val(val);
				$.rating._change.apply(this,[width]);
			}
		},
		value : function(val){
			if(val){
				opts = $(this).data('options');
				$(this).data('$input').val(val);
				width = (val/opts.maxValue)*$(this).width();
				$.rating._change.apply(this,[width]);
			}else{
				return $(this).data('$input').val();
			}
		},
		readonly : function(val){
			opts = $(this).data('options');
			opts.readonly = val;
			$(this).data('options',opts);
		},
		options : {
			value:2,
			maxValue:10,
			step:2,
			readonly : false,
			split: 0,               // split the star into how many parts?
			starWidth: 16//,
		}
	}
	$.fn.wresize = function( f ) 
	{
		version = '1.1';
		wresize = {fired: false, width: 0};

		function resizeOnce() 
		{
			if ( $.browser.msie )
			{
				if ( ! wresize.fired )
				{
					wresize.fired = true;
				}
				else 
				{
					var version = parseInt( jQuery.browser.version, 10 );
					wresize.fired = false;
					if ( version < 7 )
					{
						return false;
					}
					else if ( version == 7 )
					{
						//a vertical resize is fired once, an horizontal resize twice
						var width = $( window ).width();
						if ( width != wresize.width )
						{
							wresize.width = width;
							return false;
						}
					}
				}
			}

			return true;
		}

		function handleWResize( e ) 
		{
			if ( resizeOnce() )
			{
				
				return f.apply(this, [e]);
			}
		}

		this.each( function() 
		{
			if ( this == window )
			{
				$( this ).resize( handleWResize );
			}
			else
			{
				$( this ).resize( f );
			}
		} );

		return this;
	};
	
/*	$.fn.loadContent= function(fCmd,opts){
		return this.each(function(n,el){
			$el = $(el)
			query = 'f='+encodeURI(fCmd)+'&'+opts.query;
			opts.complete=function(xhr){
				$el.html(xhr.responseText);
			}
			$.ajax(opts.url,$.extend({
				beforeSend : function(xhr){xhr.setRequestHeader("HTTP_X_XMLHTTP_MODE", "true");},
				data:query
				},opts)
			)
		})
	}*/

	$.fn.extend({
		insertHtml : function (str) {
			$(this).focus();
			var obj = $(this).get(0);
			if (document.selection) {
				var sel = document.selection.createRange();
				sel.text = str;
			} else if (typeof obj.selectionStart === 'number' && typeof obj.selectionEnd === 'number') {
				var startPos = obj.selectionStart,
					endPos = obj.selectionEnd,
					cursorPos = startPos,
					tmpStr = obj.value;
				obj.value = tmpStr.substring(0, startPos) + str + tmpStr.substring(endPos, tmpStr.length);
				cursorPos += str.length;
				obj.selectionStart = obj.selectionEnd = cursorPos;
			} else {
				obj.value += str;
			}
		}
		,
		loadContent: function(fCmd,opts){
			opts = opts || {};
			return this.each(function(n,el){
				$el = $(el);
				opts.async = false;
				opts.complete=function(xhr){
						$el.html(xhr.responseText);
					};
				$.doCmd(fCmd,opts)
			});

		}
	});
	$.extend({
		charLen : function(s){
			return s.replace(/[^\x00-xff]/g,"**").length;
		},
		moveOptions : function(_source,_target,_copy){
			$('#'+_source).find("option:selected").each(function(n,el){
				if(_target!=null){
					if($('#'+_target).find('option[value='+$(this).val()+']').length<=0)
						$('#'+_target).append('<option value="'+$(this).val()+'">'+$(this).html()+'</option>');
				}
				if(_copy==0)$(this).remove()
			})
		},
		mutiOptions : function(_source){
			
			var rtnValue =''
			$('#'+_source+ ' option').each(function(){this.selected=true;})
			return rtnValue;
		},
		
		getCheckbox : function(checkbox,bAlert){
			var rtnValue = '';
			$('input[name='+checkbox+']').each(function(){	
				if(this.checked&&!this.disabled){
					rtnValue+=this.value+',';
				}})
			if(rtnValue==''&&bAlert) alert("请至少选择一个记录");
			return ((rtnValue=='')?false:rtnValue);
		},
　　		doCmd : function(fCmd,opts){
			opts = opts || {};
			isConfirm = opts.confirm || false;
			query = opts.query || '';
			
			/* CheckBox */
			if(opts.checkbox!=null){
				var returnVal = $.getCheckbox(opts.checkbox);
				if(!returnVal){
					alert("请至少选择一个记录");
					return;
				}
				query += '&'+opts.checkbox+'='+returnVal;
			}
			
			if(fCmd.indexOf('删除')>-1) isConfirm = true;
			title = opts.title || fCmd;
			if(isConfirm){
				if(!window.confirm('您是否确定['+title+']？'))
				return false;
			}
			 			
			method = opts.method || 'get';
			if(method=='post'){
				var url = (opts.url||location.pathname)+'?f='+encodeURI(fCmd);
			}else{
				var url = (opts.url||location.pathname);
				query = 'f='+encodeURI(fCmd)+'&'+query
			}
			var bAlert = opts.alert || false,
				docomplete = opts.complete || function(xhr){
					$._result(xhr.responseText,opts.success,opts.error || alert)
				}	
			$.ajax({
				type:method,
				async:opts.async,
				beforeSend : function(xhr){xhr.setRequestHeader("HTTP_X_XMLHTTP_MODE", "true");},
				url:url,
				data: query,
				complete:docomplete			
				})
		},
		_result : function(fC,callback,error){
			if(!fC){alert("提交服务器操作失败,请联系管理员!!!     ");return false;}
			var code = fC.split(",")[0] ;		//返回的代码
			var message = fC.split(",")[1] ;	//返回的信息
			var value = fC.split(",")[2];		//附加值,用于表单验证,返回input 的ID
			try{
				switch (code){
					case '1':
					case '2':
						var info= message || "操作成功!!!";
						//if(message && message.length>0) info = message;
						//是否回显提示
						if($.isFunction(callback))	callback(code,info);
						return true;
						break;
					case '101':
						if(message){
							error(message + "("+ code + ")");
						}else{
							error(code);
						}
						/*if(value&& $(value)) $(value).focus();*/
						return false;
					case '102':
						error('找不到此更新的内容！！！('+code+')');
						return false;
					case '103': 
						error('验证码填写出错，请检查！！！('+code+')');
						return false;
						break;
					case '104':
						error('您提交的请求已经收到,请不要重复提交！！！('+code+')');
						return false;
					case '403': 
						error('您还未登录或是登录超时！！！('+code+')');
						return false;
						break;
					default:
						if(message){
							error(message + "("+ code + ")");
						}else{
							error(code);
						}
						return false;
				}
			}catch(e){error(e.message);}
			return false;
		}
	}); 
})(jQuery);

Date.prototype.format = function(mask) {  
  
    var d = this;  
  
    var zeroize = function (value, length) {  
  
        if (!length) length = 2;  
  
        value = String(value);  
  
        for (var i = 0, zeros = ''; i < (length - value.length); i++) {  
  
            zeros += '0';  
  
        }  
  
        return zeros + value;  
  
    };    
  
    return mask.replace(/"[^"]*"|'[^']*'|\b(?:d{1,4}|m{1,4}|yy(?:yy)?|([hHMstT])\1?|[lLZ])\b/g, function($0) {  
  
        switch($0) {  
  
            case 'd':   return d.getDate();  
  
            case 'dd':  return zeroize(d.getDate());  
  
            case 'ddd': return ['Sun','Mon','Tue','Wed','Thr','Fri','Sat'][d.getDay()];  
  
            case 'dddd':    return ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'][d.getDay()];  
  
            case 'M':   return d.getMonth() + 1;  
  
            case 'MM':  return zeroize(d.getMonth() + 1);  
  
            case 'MMM': return ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'][d.getMonth()];  
  
            case 'MMMM':    return ['January','February','March','April','May','June','July','August','September','October','November','December'][d.getMonth()];  
  
            case 'yy':  return String(d.getFullYear()).substr(2);  
  
            case 'yyyy':    return d.getFullYear();  
  
            case 'h':   return d.getHours() % 12 || 12;  
  
            case 'hh':  return zeroize(d.getHours() % 12 || 12);  
  
            case 'H':   return d.getHours();  
  
            case 'HH':  return zeroize(d.getHours());  
  
            case 'm':   return d.getMinutes();  
  
            case 'mm':  return zeroize(d.getMinutes());  
  
            case 's':   return d.getSeconds();  
  
            case 'ss':  return zeroize(d.getSeconds());  
  
            case 'l':   return zeroize(d.getMilliseconds(), 3);  
  
            case 'L':   var m = d.getMilliseconds();  
  
                    if (m > 99) m = Math.round(m / 10);  
  
                    return zeroize(m);  
  
            case 'tt':  return d.getHours() < 12 ? 'am' : 'pm';  
  
            case 'TT':  return d.getHours() < 12 ? 'AM' : 'PM';  
  
            case 'Z':   return d.toUTCString().match(/[A-Z]+$/);  
  
            // Return quoted strings with the surrounding quotes removed  
  
            default:    return $0.substr(1, $0.length - 2);  
  
        }  
  
    });  
  
}; 